﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = T("Admin.Product.Create.Title");
    Layout = "~/views/shared/_AdminLayout.cshtml";
}

<h4>@T("Admin.Product.Create.Title")</h4>

@Html.Partial("_ProductInfo_Create")



@{
    var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}



   
@Styles.Render("~/content/fineuploader/fineuploader-3.5.0.css")
@Scripts.Render("~/content/fineuploader/fineuploader-3.5.0.js")
@Scripts.Render("~/content/fineuploader/iframe.xss.response-3.5.0.js")

@section scripts{
    
    

    @Scripts.Render("~/content/editor/tinymce/tinymce.js")

<script>
    var init = @jsonStr;


    

    function ProductModel() {
        var self = this;
        self.Id = ko.observable(init.Id);
        self.ProductName = ko.observable(init.ProductName).extend({ 
            required: { message: '@T("Admin.Product.NeedProductName")' },
            maxLength: 400
        });

        self.ShortDescription= ko.observable(init.ShortDescription);
        
        self.FullDescription= ko.observable(init.FullDescription).extend({ 
            required: { message: '@T("Admin.Product.NeedFullDescription")' },
        });

        self.StockQuantity = ko.observable(init.StockQuantity).extend({
            required: { message: '@T("Admin.ProductVariant.NeedStockQuantity")' },
              digit: { message: '@T("Admin.ProductVariant.StockQuantityFormatError")' }
        });
        
        self.Price = ko.observable(init.Price).extend({
            required: { message: '@T("Admin.ProductVariant.NeedPrice")' },
           number: { message: '@T("Admin.ProductVariant.PriceFormatError")' }
        });

        self.IsPublished = ko.observable(init.IsPublished);


        self.PictureId = ko.observable(init.PictureId).extend({
            notEqual:{ message: '@T("Admin.ProductVariant.NeedPicture")',params:0}
         });
        self.PictureIds = ko.observableArray([]);
        self.PictureUrl = ko.observable(init.PicUrl);
        
        self.ErrorMessage = ko.observable(null);
        self.SaveProductInfo = function() {
            self.FullDescription(tinyMCE.activeEditor.getContent());
          
            if (this.isValid()) {

                self.PictureIds.push(self.PictureId);
                
                $.ajax({
                    url: "/api/ProductApi/InsertProductInfo",
                    type: 'post',
                    data: ko.toJSON(self),
                    contentType: 'application/json',
                    success: function(result) {

                        if (result.Status == "0") {
                           
//                            alert("保存成功");
                            location.href = '/admin/Product/Edit?productId=' + result.Id; 
                        } else {
                           // alert("保存失败：" + result.Msg);

                            toastr.error(result.Message);
                        }
                    }
                });
            } else {
                
                self.errors.showAllMessages();
            }

        
        };

    }

    var vm = new ProductModel();
    vm.errors = ko.validation.group(vm);
    ko.applyBindings(vm);


</script>
    <script type="text/javascript">

        //Notes: autosave plugin is disabled
        (function () {

            tinyMCE.init({

                width:544,
                selector: "#FullDescription",
          
                verify_html: false,
                plugins: [
     "advlist autolink lists link image charmap print preview anchor",
     "searchreplace visualblocks code fullscreen",
     "insertdatetime media table contextmenu paste jbimages"
                ],
                toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages",
               

            });

        })();


        $(document).ready(function() {

            var errorHandler = function(event, id, fileName, reason) {
                qq.log("id: " + id + ", fileName: " + fileName + ", reason: " + reason);
            };


            var uploader2 = new qq.FineUploader({
                element: $('#manualUploadModeExample')[0],
                autoUpload: true,
                uploadButtonText: "Select Files",
                request: {
                    endpoint: "/FineUploader/UploadFile",
                    params: { "extraParam1": "true", "picSize": "100" }
                },
                callbacks: {
                    onError: errorHandler,
                    onComplete: function(id, fileName, responseJSON) {
                        if (responseJSON.success) {
                            vm.PictureId(responseJSON.OriginalPictureId);
                            vm.PictureUrl(responseJSON.NewPictureUrl);
                        }
                    }
                }
            });
        });
</script>


    }